/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fallen.deck.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author Carlos S. Moreno <CarlosS.Moreno@gmail.com>
 */
public class Deck implements Comparable, Serializable {

    private long id;
    private String name;
    private Card demon;
    private List<Card> cards;
    private String notes;

    public Deck() {
        this.name = "No Name";
        initializeVariables();
    }

    private void initializeVariables() {
        cards = new ArrayList<Card>();
        notes = "";
    }

    public Deck(String name, Card demon) {
        this.name = name;
        this.demon = demon;
        initializeVariables();
    }

    /**
     * @return the id
     */
    public long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the demon
     */
    public Card getDemon() {
        return demon;
    }

    /**
     * @param demon the demon to set
     */
    public void setDemon(Card demon) {
        this.demon = demon;
    }

    /**
     * @return the cards
     */
    public List getCards() {
        return cards;
    }

    /**
     * @param cards the cards to set
     */
    public void setCards(List cards) {
        this.cards = cards;
    }

    public String toString() {
        return getName();
    }

    @Override
    public int compareTo(Object o) {
        if (!(o instanceof Deck)) {
            return -1;
        }
        return this.getName().compareTo(((Deck) o).getName());
    }

    /**
     * @return the notes
     */
    public String getNotes() {
        return notes;
    }

    /**
     * @param notes the notes to set
     */
    public void setNotes(String notes) {
        this.notes = notes;
    }

    /**
     * Returns an array with the cards and the times each card is repeted on the
     * deck
     * @return Object[Cantity of Diferents Cards][2] = {Card, [times it card appears]}
     */
    public Object[][] getRows() {
        ArrayList<Card> rowCards = new ArrayList<Card>();
        ArrayList<Integer> rowCant = new ArrayList<Integer>();
        Card prev = null;
        int i = -1;
        for (Card card : cards) {
            if (prev == null || prev.compareTo(card) != 0)
            {
                rowCards.add(card);
                rowCant.add(1);
                i++;
                prev = card;
            }
            else
            {
                rowCant.set(i, rowCant.get(i)+1);
            }
        }
        Object[][] result = new Object[rowCards.size()][2];
        i = -1;
        for(Card card : rowCards)
        {
            result[++i][0] = card;
            result[i][1] = rowCant.get(i);
        }
        return result;
    }
    
    public Deck clone()
    {
        Deck cloneDeck = new Deck(name, demon);
        cloneDeck.setNotes(notes);
        List clonedCards = new ArrayList(cards);
        cloneDeck.setCards(clonedCards);
        
        return cloneDeck;
    }
    
    public String toPrityString()
    {
        String sDeck = "Mazo: " + name + "\n\n";
        sDeck += "Demonio: " + demon.getName() +"(" 
                + demon.getExpansion().getName() + ")\n\n";
        sDeck += "Cartas("+cards.size()+"): \n";
        
        Object[][] deckRows = this.getRows();
        for (int i = 0; i < deckRows.length; i++) {
            Card card = (Card) deckRows[i][0];
            sDeck += "\t"+card.getName() + " x "+ deckRows[i][1] + "\n";
        }
        return sDeck;
    }
    public String toForumString()
    {
        String sDeck = "[b]Mazo:[/b] " + name + "\n\n";
        sDeck += "[b]Demonio:[/b] " + demon.getName() +"(" 
                + demon.getExpansion().getName() + ")\n\n";
        sDeck += "[b]Cartas("+cards.size()+"):[/b] \n[list]";
        
        Object[][] deckRows = this.getRows();
        for (int i = 0; i < deckRows.length; i++) {
            Card card = (Card) deckRows[i][0];
            sDeck += "\t[*]"+card.getName() + " x "+ deckRows[i][1] + "\n";
        }
        sDeck +="[/list]";
        return sDeck;
    }
}
